﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Collector.Device
{
    public class SerialPortImpl
    {
        static string TAG = "SerialPortImpl:";
        private ComPortPack mComPort = null;
        private string mPortName;

        public ComPortPack SerialPort
        {
            get { return mComPort; }
        }

        public string PortName
        {
            get { return mPortName; }
        }
        public bool open(PortDataReceivedEventHandle handle)
        {
            bool isComPortOpen = false;
            string portInfo = "";
            string portName = "";

            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PnPEntity"))
            {
                var hardInfos = searcher.Get();
                foreach (var hardInfo in hardInfos)
                {
                    portInfo = hardInfo.Properties["DeviceID"].Value.ToString();
                    if (!string.IsNullOrEmpty(portInfo) && portInfo.Contains("VID_28E9&PID_018A"))
                    {
                        //string strComName = hardInfo.Properties["Name"].Value.ToString();
                        portName = Regex.Replace(hardInfo.Properties["Name"].Value.ToString(), @"(.*\()(.*)(\).*)", "$2").Trim();
                        Log.Debug(TAG + string.Format("DeviceName:{0},DeviceID:{1}", portName, portInfo));//打印串口设备名称及串口号
                    }
                }
            }

            if (string.IsNullOrEmpty(portName))
            {
                Log.Debug(TAG + "No found serialport.");
                return false;
            }

            mPortName = portName;

            // 配置串口，按照115200-8-1-无的方式配置
            ComPortPack.SerialPortModel model = new ComPortPack.SerialPortModel();
            model.PortName = portName;
            model.BaudRate = 115200;
            model.Parity = Parity.None;
            model.StopBits = StopBits.One;
            model.DataBits = 8;

            mComPort = new ComPortPack(model);

            // open SerPort
            if (mComPort != null && !mComPort.Open())
                return false;

            if (!mComPort.IsOpen())
            {
                return false;
            }

            // 设置serialport received callback function
            mComPort.Received += handle;
            return true;
        }

        public void close(PortDataReceivedEventHandle handle)
        {
            if (null != mComPort)
            {
                Log.Debug(TAG + "close() Release");
                mComPort.Received -= handle; // mDCManager.SerialPortReceivedCallback;
                if (mComPort.IsOpen())
                {
                    Log.Debug(TAG + "ReleaseSerialPort close serialport");
                    mComPort.Close();
                    mComPort = null;
                }
            }
        }
    }
}
